******************************************************************************************
********************************* Set Working Directory **********************************
******************************************************************************************
cd "/Users/vrhopkins/Desktop"

******************************************************************************************
********************************** Analysis (Short-Run) **********************************
**************************** Assuming Mean Sectional Lobbying ****************************
******************************************************************************************

clear
use "HPK_2017.dta"

xtset pagenda year, yearly
xtreg d.spendm d.l.spendm c.d.mip##c.lobby_sec c.d.mip##c.lobby_cau c.l.d.mip##c.l.lobby_sec c.l.d.mip##c.l.lobby_cau i.year, re robust

gen MVZ = .
gen conbx = .
gen convx = .

forval i = 1/44 {
    local MVZ100 = (`i'-1) / 100
    nlcom _b[d.mip] + `MVZ100' * _b[c.d.mip#c.lobby_cau] + (0.0457509 * _b[c.d.mip#c.lobby_sec]) , level(95)
    mat bx = r(b)
	mat vx = r(V)
	replace MVZ = `MVZ100' in `i'
    replace conbx = bx[1,1] in `i'
	replace convx = vx[1,1] in `i'
}
*

gen consx = sqrt(convx)
gen ax=1.96*consx
gen upperx=conbx+ax
gen lowerx=conbx-ax

**Create rug plot
gen where=-600
gen pipe = "|"
egen tag_lobby_cau = tag(lobby_cau)

gen yline=0

#delimit ;
graph twoway hist lobby_cau, width(0.02) percent color(gs14) yaxis(2)
		||   scatter where lobby_cau if tag_lobby_cau, plotr(m(b 4)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off)
	    ||   line conbx   MVZ, clpattern(solid) clwidth(medium) clcolor(black) yaxis(1)
        ||   line upperx  MVZ, clpattern(dash) clwidth(thin) clcolor(black)
        ||   line lowerx  MVZ, clpattern(dash) clwidth(thin) clcolor(black)
	    ||   line yline  MVZ,  clwidth(thin) clcolor(black) clpattern(solid) 
	    ||   ,  
             xlabel(0 (.2) .43, nogrid labsize(3))
		     ylabel(-600 (200) 800, axis(1) nogrid labsize(3))
		     ylabel(0 (20) 80, axis(2) nogrid labsize(3)) 
	         yscale(noline alt)
		     yscale(noline alt axis(2))	
             xscale(noline)
             legend(off)
		     title("(a) Short-Run Marginal Effect of Voter Attention on Federal Spending ($)" , size(3.5))
			 yline(0, lcolor(black) lwidth(thin)) 
             xtitle("Cause Group Pressure" , size(3.5)  )
             ytitle("Marginal Effect of Increase in Voter Attention" , axis(1) size(3.5))
			 ytitle("Percentage of Observations" , axis(2) size(3.5) orientation(rvertical))
             xsca(titlegap(2))
             ysca(titlegap(2))
			 scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white))
			 note("Coefficient on product term is 1980.473 (Robust SE=481.108, t-ratio=4.12)", size(3) position(6));
#delimit cr
graph save figure1a.gph, replace
			 
** Substantive significance (short-run)
** Calculate range of dy/dx estimate from 0 pressure to maximum and two-SD  
margins, dydx(d.mip) at(lobby_cau=(0 (.01) .43)) force
** From 0 to 0.43 (maximum), 851.6
** From 0 to .17 (two SDs), 336.68

** Substantive significance 
sum lobby_cau if e(sample)==1
sum lobby_cau if lobby_cau > 0 & e(sample)==1
sum lobby_cau if lobby_cau > .11 & e(sample)==1
di 60/130
di 60/285
* Positive effects in 46.1% of cases where cause groups are active, and 21% across the full sample.

** Statistical significance
sum lobby_cau if lobby_cau > .19 & e(sample)==1
di 28/285
* Positive and significant effects in 23.3% of cases where cause groups are active, and 10% across the full sample.

******************************************************************************************
*********************************** Analysis (Long-Run) **********************************
**************************** Assuming Mean Sectional Lobbying ****************************
******************************************************************************************

clear
use "HPK_2017.dta"

xtset pagenda year, yearly
xtreg d.spendm d.l.spendm c.d.mip##c.lobby_sec c.d.mip##c.lobby_cau c.l.d.mip##c.l.lobby_sec c.l.d.mip##c.l.lobby_cau i.year, re robust

di (_b[c.d.mip#c.lobby_cau]+_b[cL.d.mip#cL.lobby_cau])/(1-_b[l.d.spendm])
testnl (_b[c.d.mip#c.lobby_cau]+_b[cL.d.mip#cL.lobby_cau])/(1-_b[l.d.spendm]) = 0

di (_b[c.d.mip#c.lobby_sec]+_b[cL.d.mip#cL.lobby_sec])/(1-_b[l.d.spendm])
testnl (_b[c.d.mip#c.lobby_sec]+_b[cL.d.mip#cL.lobby_sec])/(1-_b[l.d.spendm]) = 0


gen MVZ = .
gen conbx = .
gen convx = .

forval i = 1/44 {
    local MVZ100 = (`i'-1) / 100
    nlcom (_b[d.mip] + _b[d.l.mip] + `MVZ100'*_b[c.d.mip#c.lobby_cau] + `MVZ100'*_b[c.l.d.mip#c.l.lobby_cau] + (0.0457509 * _b[c.d.mip#c.lobby_sec]) + (0.0457509 * _b[c.d.l.mip#c.l.lobby_sec]))/(1-_b[d.l.spendm]), level(95)
    mat bx = r(b)
	mat vx = r(V)
	replace MVZ = `MVZ100' in `i'
    replace conbx = bx[1,1] in `i'
	replace convx = vx[1,1] in `i'
}
*

gen consx = sqrt(convx)
gen ax=1.96*consx
gen upperx=conbx+ax
gen lowerx=conbx-ax

**Create rug plot
gen where=-600
gen pipe = "|"
egen tag_lobby_cau = tag(lobby_cau)

gen yline=0

#delimit ;
graph twoway hist lobby_cau, width(0.02) percent color(gs14) yaxis(2)
		||   scatter where lobby_cau if tag_lobby_cau, plotr(m(b 4)) ms(none) mlabcolor(gs5) mlabel(pipe) mlabpos(6) legend(off)
	    ||   line conbx   MVZ, clpattern(solid) clwidth(medium) clcolor(black) yaxis(1)
        ||   line upperx  MVZ, clpattern(dash) clwidth(thin) clcolor(black)
        ||   line lowerx  MVZ, clpattern(dash) clwidth(thin) clcolor(black)
	    ||   line yline  MVZ,  clwidth(thin) clcolor(black) clpattern(solid) 
	    ||   ,  
             xlabel(0 (.2) .43, nogrid labsize(3))
		     ylabel(-600 (200) 800, axis(1) nogrid labsize(3))
		     ylabel(0 (20) 80, axis(2) nogrid labsize(3)) 
	         yscale(noline alt)
		     yscale(noline alt axis(2))	
             xscale(noline)
             legend(off)
		     title("(b) Long-Run Marginal Effect of Voter Attention on Federal Spending ($)" , size(3.5))
			 yline(0, lcolor(black) lwidth(thin)) 
             xtitle("Cause Group Pressure" , size(3.5)  )
             ytitle("Marginal Effect of Increase in Voter Attention" , axis(1) size(3.5))
			 ytitle("Percentage of Observations" , axis(2) size(3.5) orientation(rvertical))
             xsca(titlegap(2))
             ysca(titlegap(2))
			 scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white))
			 note("Coefficient on long-run product term is 1792.524 (Robust SE=686.83, t-ratio=2.61)", size(3) position(6));
#delimit cr
graph save figure1b.gph, replace

*** Combine Figures 1(a) and 1(b)
#delimit ;
graph combine figure1a.gph figure1b.gph
, 
cols(1) ysize(10) scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white));
#delimit cr
graph save figures1ab.gph, replace
graph export figures1ab.eps, replace

** Substantive significance 
sum lobby_cau if e(sample)==1
sum lobby_cau if lobby_cau > 0 & e(sample)==1
sum lobby_cau if lobby_cau > .14 & e(sample)==1
di 44/130
di 44/285
* Positive effects in 33.8% of cases where cause groups are active, and 15% across the full sample.

** Statistical significance
sum lobby_cau if lobby_cau > .21 & e(sample)==1
di 22/285
* Positive and significant effects in 23.3% of cases where cause groups are active, and 7% across the full sample.

******************************************************************************************
********************************* Stata Results in LaTeX *********************************
******************************************************************************************

clear
use "HPK_2017.dta"

xtset pagenda year, yearly

gen spendm_d = d.spendm
gen spendm_d_lag = l.d.spendm
gen lobby_cau_lag = l.lobby_cau
gen lobby_sec_lag = l.lobby_sec
gen mip_d = d.mip
gen mip_d_lag = d.l.mip
gen mip_dxlobby_cau = d.mip*lobby_cau
gen mip_dxlobby_sec = d.mip*lobby_sec
gen mip_d_lagxlobby_sec_lag = d.l.mip*l.lobby_sec
gen mip_d_lagxlobby_cau_lag = d.l.mip*l.lobby_cau

label variable spendm_d "Change in Federal Spending"
label variable spendm_d_lag "Change in Federal Spending_{t-1}"
label variable lobby_sec "Sectional Group Pressure"
label variable lobby_sec_lag "Sectional Group Pressure_{t-1}"
label variable lobby_cau "Cause Group Pressure"
label variable lobby_cau_lag "Cause Group Pressure_{t-1}"
label variable mip_d "Change in Voter Attention"
label variable mip_d_lag "Change in Voter Attention_{t-1}"
label variable mip_dxlobby_sec "Change in Voter Attention x Sectional Group Pressure"
label variable mip_d_lagxlobby_sec_lag "Change in Voter Attention_{t-1} x Sectional Group Pressure_{t-1}"
label variable mip_dxlobby_cau "Change in Voter Attention x Cause Group Pressure"
label variable mip_d_lagxlobby_cau_lag "Change in Voter Attention_{t-1} x Cause Group Pressure_{t-1}"

** Regression Tables
eststo clear
tab year, gen(yr)
eststo: quietly xtreg spendm_d spendm_d_lag mip_d lobby_sec lobby_cau mip_dxlobby_sec mip_dxlobby_cau mip_d_lag lobby_sec_lag lobby_cau_lag mip_d_lagxlobby_sec_lag mip_d_lagxlobby_cau_lag yr1-yr20, re robust
est store A

** Table 1
esttab A using table1.tex, replace ///
drop(yr* _cons) ///
b(3) se(3) ///
title(The impact of voter attention on government spending. \\ Dependent variable: Changes in Federal Government spending) ///
nonumbers mtitles("$\hat{\beta}$ (Robust SE)") ///
note("Cell entries represent unstandardized b coefficients with robust standard errors in parentheses.") ///
addnote("Model includes dummy variables for each year.") ///
star(* 0.05)  ///
label compress width(1.0\hsize) ///
alignment(ccc) ///
stats(N r2_w, fmt(0 3) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"Observations"' `"\(R^{2}\)"'))

******************************************************************************************
************************************ Long Run Tables *************************************
******************************************************************************************
clear
use "HPK_2017.dta"

xtset pagenda year, yearly

xtreg d.spendm d.l.spendm c.d.mip##c.lobby_sec c.d.mip##c.lobby_cau c.l.d.mip##c.l.lobby_sec c.l.d.mip##c.l.lobby_cau i.year year, re robust
di (_b[c.d.mip#c.lobby_cau]+_b[cL.d.mip#cL.lobby_cau])/(1-_b[l.d.spendm])
testnl (_b[c.d.mip#c.lobby_cau]+_b[cL.d.mip#cL.lobby_cau])/(1-_b[l.d.spendm]) = 0
mat lr_cau = (r(R),r(chi2),r(p))

xtreg d.spendm d.l.spendm c.d.mip##c.lobby_sec c.d.mip##c.lobby_cau c.l.d.mip##c.l.lobby_sec c.l.d.mip##c.l.lobby_cau i.year year, re robust
testnl (_b[c.d.mip#c.lobby_sec]+_b[cL.d.mip#cL.lobby_sec])/(1-_b[l.d.spendm]) = 0
mat lr_sec = (r(R),r(chi2),r(p))

svmat lr_cau, name(lr_cau)
svmat lr_sec, name(lr_sec)

gen lr_caua = round(lr_cau1, .001)
gen lr_caub = round(lr_cau2, .001)
gen lr_cauc = round(lr_cau3, .001)
gen lr_seca = round(lr_sec1, .001)
gen lr_secb = round(lr_sec2, .001)
gen lr_secc = round(lr_sec3, .001)

mkmat lr_caua, matrix(lr_caua)
mkmat lr_caub, matrix(lr_caub)
mkmat lr_cauc, matrix(lr_cauc)
mkmat lr_seca, matrix(lr_seca)
mkmat lr_secb, matrix(lr_secb)
mkmat lr_secc, matrix(lr_secc)

mat longrunfx = (lr_caua[1,1],lr_caub[1,1], lr_cauc[1,1] \ lr_seca[1,1],lr_secb[1,1], lr_secc[1,1])

matlist longrunfx

esttab matrix(longrunfx) using longrun.tex, replace align(ccc) b(3) ///
title(The impact of voter attention on government spending. \\ Dependent variable: Changes in Federal Government spending) ///
mlabels(none) collabels("Long-Run Effects" "Chi-Sq." "P-value") ///
varlabels(r1 "$\Delta$ Voter Attention x Cause Group Pressure" r2 "$\Delta$ Voter Attention x Sectional Group Pressure")

**To calculate SEs for marginal effects plots
nlcom (_b[c.d.mip#c.lobby_cau]+_b[cL.d.mip#cL.lobby_cau])/(1-_b[l.d.spendm])
nlcom (_b[c.d.mip#c.lobby_sec]+_b[cL.d.mip#cL.lobby_sec])/(1-_b[l.d.spendm])
